/*******************************************************
��Ʒ�ͺ�,IC,Vlcm,Vlcd,Duty,Bias,��ѹ
YMC12864-472AAABUGN ,Ic:ST7567-G4B, 1/65DUTY,1/9BIAS
VDD1=3.3V, VOP=12V,4-pin serial
SEG:seg0~seg127	COM:com0~com63
********************************************************/

#include "C200114-01A_C2.h"

void delay_ms(uint32_t time_ms) {
	HAL_Delay(time_ms);
}

void delay_ss(uint32_t time_ss) {
	for(uint32_t i = 0; i < time_ss; i++){
		HAL_Delay(1000);
	}
	
	
}

unsigned char i;

void write_com(unsigned char byte) {
    

    //cs = 0;
	HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_RESET);

    //rs = 0;
	HAL_GPIO_WritePin(LCD_A0_GPIO_Port, LCD_A0_Pin, GPIO_PIN_RESET);
    for(i = 0; i < 8; i++) {
        //sck = 0;
		HAL_GPIO_WritePin(LCD_SCK_GPIO_Port, LCD_SCK_Pin, GPIO_PIN_RESET);
        //_nop_();
        //_nop_();
        //sda = (bit)(byte & 0x80);
		HAL_GPIO_WritePin(LCD_SDA_GPIO_Port, LCD_SDA_Pin, (byte & 0x80)? GPIO_PIN_SET : GPIO_PIN_RESET);
        byte = byte << 1;
        //sck = 1;
		HAL_GPIO_WritePin(LCD_SCK_GPIO_Port, LCD_SCK_Pin, GPIO_PIN_SET);
        //_nop_();
        //_nop_();
    }

    //cs = 1;
	HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_SET);
}

//serial 4-SPIд˽ߝû
void write_dat(unsigned char byte) {
    //cs = 0;
	HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_RESET);

    //rs = 1;
	HAL_GPIO_WritePin(LCD_A0_GPIO_Port, LCD_A0_Pin, GPIO_PIN_SET);
    for(i = 0; i < 8; i++) {
        //sck = 0;
		HAL_GPIO_WritePin(LCD_SCK_GPIO_Port, LCD_SCK_Pin, GPIO_PIN_RESET);
        //_nop_();
        //_nop_();
        //sda = (bit)(byte & 0x80);
		HAL_GPIO_WritePin(LCD_SDA_GPIO_Port, LCD_SDA_Pin, (byte & 0x80)? GPIO_PIN_SET : GPIO_PIN_RESET);
        byte = byte << 1;
        //sck = 1;
		HAL_GPIO_WritePin(LCD_SCK_GPIO_Port, LCD_SCK_Pin, GPIO_PIN_SET);
        //_nop_();
        //_nop_();
    }

    //cs = 1;
	HAL_GPIO_WritePin(LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_SET);
}


//#include "4spi.h"  //cs cd sclk sda


extern const unsigned char gImage_200114_1[11400];
extern const unsigned char gImage_200114_2[11400];


void fill_DDRAM(unsigned char dat) {
	unsigned char i,j;
	
	write_com(0x2c);
	
	for (i=0;i<114;i++) {
	
		for (j=0;j<34;j++) {
				write_dat(dat);
				write_dat(dat);
				write_dat(dat);

		}
	}
}


void fill_array(unsigned char * dat) {
	unsigned char i,j;
	
	write_com(0x2c);
	
	for (i=0;i<114;i++) {
	
		for (j=0;j<34;j++) {
			write_dat(*dat++);
			write_dat(*dat++);
			write_dat(*dat++);
		}
	}
}

void fill_DDRAM3(unsigned char dat1, unsigned char dat2, unsigned char dat3) {
	unsigned char i,j;
	
	write_com(0x2c);
	
	for (i=0;i<114;i++) {
	
		for (j=0;j<34;j++) {
				write_dat(dat1);
				write_dat(dat2);
				write_dat(dat3);

		}
	}
}

void fill_DDRAM_Line(unsigned char dat1, unsigned char dat2, unsigned char dat3) {
	unsigned char i,j,k;
	
	write_com(0x2c);
	
	for (i=0;i<114;i++) {
	
		for (j=0;j<34;j++) {
		
			k = i%3;
				switch(k) {
					case 0:
					write_dat(dat1);
					write_dat(dat1);
					write_dat(dat1);
					break;
					
					case 1:
					write_dat(dat2);
					write_dat(dat2);
					write_dat(dat2);
					break;
					
					case 2:
					write_dat(dat3);
					write_dat(dat3);
					write_dat(dat3);
					break;
					
					default:
					break;
				}
				
			

		}
	}
}



void c200114_01a_init(void) {

    //res = 0;			//ģ�鸴λ
	HAL_GPIO_WritePin(LCD_RSTN_GPIO_Port, LCD_RSTN_Pin, GPIO_PIN_RESET);
    delay_ms(150);
    //res = 1;
	HAL_GPIO_WritePin(LCD_RSTN_GPIO_Port, LCD_RSTN_Pin, GPIO_PIN_SET);
    delay_ms(200);
	delay_ms(200);
	
	
    write_com(0xD7); // Disable Auto Read
    write_dat(0x9F);
    write_com(0xE0); // Enable OTP Read
    write_dat(0x00);
    delay_ms(10);
    write_com(0xE3); // OTP Up-Load
    delay_ms(20);
    write_com(0xE1); // OTP Control Out
    write_com(0x11); // Sleep Out
    write_com(0x28); // Display OFF
    delay_ms(50);
    write_com(0xC0); // Vop = B9h
    write_dat(0xeb);									//
    write_dat(0x00);
    write_com(0xC3); // BIAS = 1/9
    write_dat(0x04);
    write_com(0xC4); // Booster = x8
    write_dat(0x04);
    write_com(0xD0); // Enable Analog Circuit
    write_dat(0x1D);
    write_com(0xB5); // N-Line = 0
    write_dat(0x07);
    write_com(0x38); // Enable DDRAM
    write_com(0x3A); // Enable DDRAM Interface
    write_dat(0x03);
    write_com(0x36); // Scan Direction Setting
    write_dat(0x00);
    write_com(0xB0); // Duty Setting
    write_dat(0x72);
	
	//
	write_com(0x12); // Set partial mode PTL=0: Partial mode on
	
    write_com(0xB4); // Partial Display
    write_dat(0xA0);
    write_com(0x30); // Partial Display Area = COM0 ~ COM119
    write_dat(0x00);
    write_dat(0x00);
    write_dat(0x00);
    write_dat(0x71);
    write_com(0x20); // Display Inversion OFF
	
	//
	write_com(0xb1); // First Output COM
	write_dat(0x00);
	
	//
	write_com(0xf0); // Frame Rate
	write_dat(0x12);
	write_dat(0x12);
	write_dat(0x12);
	write_dat(0x12);
	
    write_com(0x2A); // Column Address Setting
    write_dat(0x00); // SEG0 -> SEG384
    write_dat(0x00);
    write_dat(0x00);
    write_dat(0x43);	//

    write_com(0x2B); // Row Address Setting
    write_dat(0x00); // COM0 -> COM160
    write_dat(0x00);
    write_dat(0x00);
    write_dat(0x71);	//
    /*//Clear_DDRAM(); // Clear whole DDRAM by ??(384 x 160 x 4)
    write_com(0x2A); // Column Address Setting
    write_dat(0x00); // SEG0 -> SEG239
    write_dat(0x00);
    write_dat(0x00);
    write_dat(0x4F);
    write_com(0x2B); // Row Address Setting
    write_dat(0x00); // COM0 -> COM120
    write_dat(0x00);
    write_dat(0x00);
    write_dat(0x78);
    //Disp_Image(); // Fill the DDRAM Data by Panel Resolution
	 //lcd_image(gImage_triangle_12864);				//ͼ��
	write_com(0x23);*/
	
    write_com(0x29); // Display ON
	
	write_com(0xC0); // Vop = B9h
    write_dat(0x29);	// default 0x29								//
    write_dat(0x01);
	
	write_com(0xC3); // Set BIAS system
    write_dat(0x05);	

	write_com(0xC4); // Set booster level
    write_dat(0x05);		
	
	write_com(0xf2); // Temperature Range
    write_dat(0x28);
	write_dat(0x41);
	write_dat(0x50);
	
	write_com(0xf4); // Temperature Gradient Compensation
    write_dat(0x22);
	write_dat(0x00);
	write_dat(0x00);
	write_dat(0xf8);
	write_dat(0x00);
	write_dat(0x32);
	write_dat(0x82);
	write_dat(0xb8);
	
	write_com(0xf9); // Gray Table
	write_dat(0x00);
	write_dat(0x03);
	write_dat(0x03);
	write_dat(0x03);
	write_dat(0x03);
	write_dat(0x0b);
	write_dat(0x0b);
	write_dat(0x0b);
	write_dat(0x10);
	write_dat(0x10);
	write_dat(0x10);
	write_dat(0x10);
	write_dat(0x12);
	write_dat(0x1c);
	write_dat(0x1c);
	write_dat(0x1c);
	
}

void test_c200114_01a (void) {
    //delay_us(0);
    delay_ms(0);
    delay_ss(0);
	
	
	
	c200114_01a_init();

	
	while(1) {
		fill_DDRAM(0x00);
		delay_ms(100);
	
		fill_DDRAM(0xff);
		delay_ss(1);
		
		
		
		
		/*fill_DDRAM3(0xf0, 0x0f, 0x00);
		delay_ms(100);
		
		fill_DDRAM3(0x0f, 0x00, 0xf0);
		delay_ms(100);
		
		fill_DDRAM3(0x00, 0xf0, 0x0f);
		delay_ms(100);
		
		fill_DDRAM_Line(0xff, 0x00, 0x00);
		delay_ms(100);
		
		fill_DDRAM_Line(0x00, 0xff, 0x00);
		delay_ms(100);
		
		fill_DDRAM_Line(0x00, 0x00, 0xff);*/
		delay_ms(100);
		
		fill_array((unsigned char*)gImage_200114_1);
		delay_ss(1);
		
		fill_array((unsigned char*)gImage_200114_2);
		delay_ss(1);
		
	}

}